﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Communergy.Common;
using Communergy.Repository.EntityFramework;

namespace Communergy.Repository
{
    public class EntityMeterRepository : IMeterRepository
    {
		#region Fields (1) 

        communergyEntities db;

		#endregion Fields 

		#region Constructors (1) 

        public EntityMeterRepository(IUnitOfWork unitOfWork)
        {
            //will return null if not communergyEntities
            db = unitOfWork as communergyEntities;
            if (db == null)
                throw new ArgumentOutOfRangeException("unitOfWork");          
        }

       

		#endregion Constructors 

		#region Methods (2) 

		// Private Methods (2) 

        private Meter CommunergyMeterFromEntityMeter(ElectricityMeter entityMeter)
        {
            if (entityMeter != null)
            {
                Meter communergyMeter = new Meter
                {
                    MeterId = (int)entityMeter.me_id,
                    MeterName = entityMeter.me_meterName,
                    MeterGuid = entityMeter.me_meterGuid.ToString(),
                    UserId = entityMeter.me_aspUserId.ToString()
                };
                return communergyMeter;
            }
            else
            {
                return null;
            }

        }

        private ElectricityMeter EntityMeterFromCommunergyMeter(Meter communergyMeter)
        {
            if (communergyMeter != null)
            {
                Guid meterGuid = new Guid(communergyMeter.MeterGuid);
                Guid userGuid = new Guid(communergyMeter.UserId);
                var entityMeter = new ElectricityMeter
                {
                    me_id = communergyMeter.MeterId,
                    me_meterGuid = meterGuid,
                    me_meterName = communergyMeter.MeterName,
                    me_aspUserId = userGuid
                };
                return entityMeter;
            }
            else
            {
                return null;
            }
        }

		#endregion Methods 

        #region IMeterRepository Members

        public Meter CreateNewMeter(Meter meterToCreate)
        {
            //using (var db = new communergyEntities(connString))
            {
                var insertMeter = EntityMeterFromCommunergyMeter(meterToCreate);
                db.AddToElectricityMeter(insertMeter);
                db.SaveChanges();
                var savedMeter = CommunergyMeterFromEntityMeter(insertMeter);
                return savedMeter;
            }
        }


        public Meter RetrieveMeterDetails(string meterId)
        {
            throw new NotImplementedException();
        }

        public void SaveMeterDetails(Meter meterToSave)
        {
            throw new NotImplementedException();
        }

        public List<Meter> GetMetersForUser(string userId)
        {
           // using (var db = new communergyEntities(connString))
            {
                Guid userGuid = new Guid(userId);
                //get meters from db
                var metersList =( from m in db.ElectricityMeter
                             where m.me_aspUserId == userGuid
                             select m).ToList(); //convert to IEnumerable
                //project into POCO objects
                var meters = from m in metersList
                             select new Meter
                             {
                                 MeterName = m.me_meterName,
                                 MeterGuid = m.me_meterGuid.ToString(),
                                 UserId = m.me_aspUserId.ToString()
                             };
                return meters.ToList();
            }
        }

        #endregion
    }
}
